<template>
  <div>
    <f-table ref='orderDrugs' rowKey='orderDetailsId' :scroll='{x:true}' :showTotal='true' handler='hyPrintOrderDetailsHandler' :tableColumns='orderDetailsColumns' :operator='operator' :params='queryParam' :api='api.hyOrderDetails' :pageSize='80' :autoLoad='false' :showPagination='false' :multiple='false'>
       <span slot='drugType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text=="ZY"'>自营</a-tag>
           <a-tag color='#FF0000' v-if='text=="ZS"'>招商</a-tag>
       </span>
      <span slot='printDrugJs' slot-scope='text, record, index' v-if='record.rowIndex!="合计"'>
         <a-input-number style='width:80px' :value='text' @change='(value)=>updateDrugJs(record,value)' />
      </span>
      <span slot='action' slot-scope='text, record' v-if='record.rowIndex!="合计"'>
          <template>
            <a @click='openPrintModal(record)'>打印</a>
          </template>
        </span>
    </f-table>
    <a-modal title='商品标签打印' v-model='printModal' width='500px' height='500px' @cancel='this.printModal=false' @ok='print'>
      打印数量:
      <a-input-number id='inputNumber' style='width:200px' v-model='printCount' :min='1' />
    </a-modal>
    <FlymePrintView ref='preView' />
  </div>

</template>

<script>
import api from "../../../js/api"
import HyOrderDescriptions from "@/views/module/haiying/order/details/hyorder_descriptions"
import FlymePrintView from "@/components/Flyme/FlymePrintDesigner/preview"

export default {
  name: "printDetails",
  components: { HyOrderDescriptions, FlymePrintView },
  data() {
    let me = this
    //操作按钮
    let operator = []
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      orderId: null,
      orderDetail: null,
      printModal: false,
      printCount: 1,
      loaded: false,
      orderDetailsColumns: [],
      operator: operator
    }
  },
  created() {
  },
  computed: {},
  methods: {
    setData(data) {
      if (data && data.orderId) {
        this.orderId = data.orderId
        this.loaded = true
        this.queryParam.orderId = data.orderId
        this.orderDetailsColumns = this.getCol(data)
        this.$refs["orderDrugs"].refresh(false)
      }
    },
    updateDrugJs(record, value) {
      record.printDrugJs = value
      console.info(value)
      this.http.post(this, {
        url: api.hyOrderDetails.updatePrintDrugJs,
        data: {
          orderDetailsId: record.orderDetailsId,
          printDrugJs: value
        },
        success: (me, data) => {
          this.$refs["orderDrugs"].refresh()
        }
      })
    },
    openPrintModal(record) {
      this.orderDetail = record
      this.printModal = true
      this.printCount = record.drugJsCount
    },
    //直接打印
    print() {
      this.http.post(this, {
        url: api.hyOrderDetails.getOutOrderPrint,
        data: {
          orderDetailsId: this.orderDetail.orderDetailsId,
          customCount: this.printCount
        },
        success: (me, data) => {
          let template = data.template
          let printDate = data.dataList
          let printTemplate = JSON.parse(template)
          this.$refs.preView.batchPrintByCount({
            title: "商品标签",
            template: printTemplate,
            dataList: printDate,
            printCount: this.printCount
          })
        }
      })
      this.printModal = false
    },
    getCol(data) {
      let saleManId = !data.saleManId
      let sendType = data.sendType
      let deptType = data.deptType
      let isZbj = false
      if (sendType == 3) {
        isZbj = true
      }
      return [
        {
          title: "商品名称",
          align: "left",
          ellipsis: true,
          width: "140px",
          dataIndex: "drugsName"
        },
        {
          title: "规格",
          align: "left",
          ellipsis: true,
          width: "120px",
          dataIndex: "drugsSpec"
        },
        {
          title: "批号",
          align: "left",
          width: "80px",
          dataIndex: "drugLotNo"
        },
        {
          title: "仓库",
          align: "left",
          width: "80px",
          dataIndex: "storeName"
        },
        {
          title: "类型",
          align: "center",
          width: "80px",
          dataIndex: "drugType",
          scopedSlots: { customRender: "drugType" }
        },
        {
          title: "销售单价",
          align: "center",
          width: "80px",
          dataIndex: "salePrice"
        },
        {
          title: "数量",
          align: "center",
          width: "80px",
          dataIndex: "saleNum"
        },
        {
          title: "销售金额",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "saleAmount"
        },
        {
          title: "件数",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "drugJs"
        },
        {
          title: "打印件数",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "printDrugJs",
          scopedSlots: { customRender: "printDrugJs" }
        },
        {
          title: "整件数",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "drugJsCount"
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "70px",
          scopedSlots: { customRender: "action" }
        }
      ]
    },
    handleSuccess() {
      this.$refs["orderDrugs"].refresh()
      this.$emit("success")
    }
  }
}
</script>